/**
 * 消息队列接收端
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct msgbuf
{
    long mtype;
    int start;
    int end;
};

int main(int argc, char **argv)
{

    if (3 > argc)
    {
        fprintf(stderr, "%s <key> <msgtype>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    key_t key = (key_t)atoi(argv[1]);
    int msgid = msgget(key, S_IRUSR| S_IWUSR | IPC_EXCL);
    if (0 > msgid)
    {
        perror("msgget error: ");
        exit(EXIT_FAILURE);
    }
    struct msgbuf msgbuf;
    if (msgrcv(msgid, &msgbuf, sizeof(struct msgbuf) - sizeof(long), atoi(argv[2]), IPC_NOWAIT) < 0)
    {
        perror("msgrcv error: ");
        exit(EXIT_FAILURE);
    }

    printf("msg id: %d\n",msgid);
    printf("type: %ld\tstart: %d\tend: %d\n",msgbuf.mtype,msgbuf.start,msgbuf.end);

    return 0;
}